/*********************************************************************************************************
** Filename	:	hash.h
** Describe	:	This is a general-purpose C language hash module
** Version	:	V1.0.0
** Author	:	Lamdonn
** Date		:	2022.11.07
********************************************************************************************************/
#include "hash.h"

/*********************************************************************************************************
** Name		:	hash_bkdr
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_bkdr(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
    unsigned int hash = 0;
     
    for (i = 0; i < size; i++)
    {
        hash = hash * seed + (*str++);
    }
     
    return (hash & 0x7FFFFFFF);
}

/*********************************************************************************************************
** Name		:	hash_ap
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_ap(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int hash = 0;
 
    for (i = 0; i < size; i++)
    {
        if ((i & 1) == 0)
        {
            hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
        }
        else
        {
            hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
        }
    }
 
    return (hash & 0x7FFFFFFF);
}

/*********************************************************************************************************
** Name		:	hash_djb
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_djb(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int hash = 5381;
 
    for (i = 0; i < size; i++)
    {
        hash += (hash << 5) + (*str++);
    }
 
    return (hash & 0x7FFFFFFF);
}

/*********************************************************************************************************
** Name		:	hash_js
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_js(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int hash = 1315423911;
 
    for (i = 0; i < size; i++)
    {
        hash ^= ((hash << 5) + (*str++) + (hash >> 2));
    }
 
    return (hash & 0x7FFFFFFF);
}

/*********************************************************************************************************
** Name		:	hash_rs
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_rs(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int b = 378551;
    unsigned int a = 63689;
    unsigned int hash = 0;
 
    for (i = 0; i < size; i++)
    {
        hash = hash * a + (*str++);
        a *= b;
    }
 
    return (hash & 0x7FFFFFFF);
}

/*********************************************************************************************************
** Name		:	hash_sdbm
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_sdbm(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int hash = 0;
 
    for (i = 0; i < size; i++)
    {
        // equivalent to: hash = 65599*hash + (*str++);
        hash = (*str++) + (hash << 6) + (hash << 16) - hash;
    }
 
    return (hash & 0x7FFFFFFF);
}

/*********************************************************************************************************
** Name		:	hash_pjw
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_pjw(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int bits_in_unigned_int = (unsigned int)(sizeof(unsigned int) * 8);
    unsigned int three_quarters      = (unsigned int)((bits_in_unigned_int  * 3) / 4);
    unsigned int one_eighth          = (unsigned int)(bits_in_unigned_int / 8);
    unsigned int high_bits           = (unsigned int)(0xFFFFFFFF) << (bits_in_unigned_int - one_eighth);
    unsigned int hash                = 0;
    unsigned int test                = 0;
 
    for (i = 0; i < size; i++)
    {
        hash = (hash << one_eighth) + (*str++);
        if ((test = hash & high_bits) != 0)
        {
            hash = ((hash ^ (test >> three_quarters)) & (~high_bits));
        }
    }
 
    return (hash & 0x7FFFFFFF);
}

/*********************************************************************************************************
** Name		:	hash_elf ***
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_elf(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int hash = 0;
    unsigned int x    = 0;
 
    for (i = 0; i < size; i++)
    {
        hash = (hash << 4) + (*str++);
        if ((x = hash & 0xF0000000L) != 0)
        {
            hash ^= (x >> 24);
            hash &= ~x;
        }
    }
 
    return (hash & 0x7FFFFFFF);
}

/*********************************************************************************************************
** Name		:	hash_dek
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_dek(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int hash = size;

    for (i = 0; i < size; i++)
    {
        hash = (hash << 5) ^ (hash >> 27) ^ (*str++);
    }

    return (hash & 0x7FFFFFFF);
}

/*********************************************************************************************************
** Name		:	hash_bp
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_bp(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int hash = size;

    for (i = 0; i < size; i++)
    {
        hash = (hash << 7) ^ (*str++);
    }

    return (hash & 0x7FFFFFFF);
}

/*********************************************************************************************************
** Name		:	hash_fnv
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_fnv(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int fnv_prime = 0x811C9DC5;
    unsigned int hash = 0;

    for (i = 0; i < size; i++)
    {
        hash *= fnv_prime;
        hash ^= (*str++);
    }

    return (hash & 0x7FFFFFFF);
}

/*********************************************************************************************************
** Name		:	hash_jdk6
** Describe	:	hash function
** Input	:	*data: data address
				size: size of data
** Output	:	hash value
********************************************************************************************************/
unsigned int hash_jdk6(void *data, int size)
{
    int i = 0;
    unsigned char *str = (unsigned char *)data;
    unsigned int hash = 0;

    for (i = 0; i < size; i++)
    {
        hash = hash * 31 + (*str++);
    }

    return (hash & 0x7FFFFFFF);
}

